package com.androidcourse.controller;

import com.androidcourse.bean.User;
import com.androidcourse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/getUserList")
    public List<User> getUserList(){

        List<User> userList = userService.getUserList();
        return userList;
    }


    @GetMapping("/getUserListXml")
    public List<User> getUserListXml(){

        List<User> userList = userService.getUserListXml();
        return userList;
    }


    //登录的接口
    @PostMapping("/userLogin")
    public Map userLogin(@RequestBody  User loginUser){
        User user = userService.userLogin(loginUser);
       Map<String, Object> data = new HashMap<>();
       if (user!=null){//登录成功
           data.put("code","000");
           data.put("msg","成功");
           data.put("userinfo",user);
       }else {//登录失败的
           data.put("code","888");
           data.put("msg","用户名或用户名不正确");
       }
        return  data;

    }

    @PostMapping("/userRegister")
    public Map userRegister(@RequestBody User registerUser){
        int result = userService.userRegister(registerUser);
         Map<String, Object> data = new HashMap<>();
        if (result!=0){
            data.put("code","00");
            data.put("msg","注册成功");
        }else {
            data.put("code","88");
            data.put("msg","注册失败");
        }
        return data;
    }


}
